{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3d6174e9-4ed8-43c1-85cf-2ce0e02bc059",
   "metadata": {},
   "source": [
    "# 数值运算操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b324a84d-b377-4779-a4ef-4addc03f6e6c",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df = pd.DataFrame([[1,2,3], [4,5,6]], index = [\"a\", \"b\"], columns=[\"A\", \"B\", \"C\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "23f55547-bfc0-41ff-959d-31ff10ef426d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A  B  C\n",
       "a  1  2  3\n",
       "b  4  5  6"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55f6135d-d338-4f48-b03c-acc692e9742c",
   "metadata": {},
   "source": [
    "## 求和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3462087b-846e-4862-b6ec-14d58ee3cef0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    5\n",
       "B    7\n",
       "C    9\n",
       "dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "259901d9-a40f-421d-9e4e-748f8a399193",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    5\n",
       "B    7\n",
       "C    9\n",
       "dtype: int64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sum(axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2bab1bcf-f557-440c-ae15-241d921d099b",
   "metadata": {},
   "source": [
    "## 求平均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "dea991aa-22c8-41cf-8dc4-1cae7b7b3a7c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    2.5\n",
       "B    3.5\n",
       "C    4.5\n",
       "dtype: float64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f46816fd-541f-4a0b-8f61-55c0950cc1fd",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    2.0\n",
       "b    5.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.mean(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "191692f8-0301-471b-9799-362e3da7d189",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    2.5\n",
       "B    3.5\n",
       "C    4.5\n",
       "dtype: float64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.median()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee463cea-1493-4386-a274-ae7fe0872266",
   "metadata": {},
   "source": [
    "## 二维统计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c6c2456c-a059-465b-a10f-6b7f5e4d44e7",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df = pd.read_csv(\"titanic/train.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "705f2bb7-960c-4149-8491-d53ea3a3b3cc",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Fare</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>PassengerId</th>\n",
       "      <td>66231.000000</td>\n",
       "      <td>-0.626966</td>\n",
       "      <td>-7.561798</td>\n",
       "      <td>138.696504</td>\n",
       "      <td>-16.325843</td>\n",
       "      <td>-0.342697</td>\n",
       "      <td>161.883369</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Survived</th>\n",
       "      <td>-0.626966</td>\n",
       "      <td>0.236772</td>\n",
       "      <td>-0.137703</td>\n",
       "      <td>-0.551296</td>\n",
       "      <td>-0.018954</td>\n",
       "      <td>0.032017</td>\n",
       "      <td>6.221787</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pclass</th>\n",
       "      <td>-7.561798</td>\n",
       "      <td>-0.137703</td>\n",
       "      <td>0.699015</td>\n",
       "      <td>-4.496004</td>\n",
       "      <td>0.076599</td>\n",
       "      <td>0.012429</td>\n",
       "      <td>-22.830196</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Age</th>\n",
       "      <td>138.696504</td>\n",
       "      <td>-0.551296</td>\n",
       "      <td>-4.496004</td>\n",
       "      <td>211.019125</td>\n",
       "      <td>-4.163334</td>\n",
       "      <td>-2.344191</td>\n",
       "      <td>73.849030</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SibSp</th>\n",
       "      <td>-16.325843</td>\n",
       "      <td>-0.018954</td>\n",
       "      <td>0.076599</td>\n",
       "      <td>-4.163334</td>\n",
       "      <td>1.216043</td>\n",
       "      <td>0.368739</td>\n",
       "      <td>8.748734</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Parch</th>\n",
       "      <td>-0.342697</td>\n",
       "      <td>0.032017</td>\n",
       "      <td>0.012429</td>\n",
       "      <td>-2.344191</td>\n",
       "      <td>0.368739</td>\n",
       "      <td>0.649728</td>\n",
       "      <td>8.661052</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Fare</th>\n",
       "      <td>161.883369</td>\n",
       "      <td>6.221787</td>\n",
       "      <td>-22.830196</td>\n",
       "      <td>73.849030</td>\n",
       "      <td>8.748734</td>\n",
       "      <td>8.661052</td>\n",
       "      <td>2469.436846</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              PassengerId  Survived     Pclass         Age      SibSp  \\\n",
       "PassengerId  66231.000000 -0.626966  -7.561798  138.696504 -16.325843   \n",
       "Survived        -0.626966  0.236772  -0.137703   -0.551296  -0.018954   \n",
       "Pclass          -7.561798 -0.137703   0.699015   -4.496004   0.076599   \n",
       "Age            138.696504 -0.551296  -4.496004  211.019125  -4.163334   \n",
       "SibSp          -16.325843 -0.018954   0.076599   -4.163334   1.216043   \n",
       "Parch           -0.342697  0.032017   0.012429   -2.344191   0.368739   \n",
       "Fare           161.883369  6.221787 -22.830196   73.849030   8.748734   \n",
       "\n",
       "                Parch         Fare  \n",
       "PassengerId -0.342697   161.883369  \n",
       "Survived     0.032017     6.221787  \n",
       "Pclass       0.012429   -22.830196  \n",
       "Age         -2.344191    73.849030  \n",
       "SibSp        0.368739     8.748734  \n",
       "Parch        0.649728     8.661052  \n",
       "Fare         8.661052  2469.436846  "
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#协方差\n",
    "df.cov(numeric_only=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "64124658-8249-496b-b4c3-cf2063afdf2e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Fare</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>PassengerId</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.005007</td>\n",
       "      <td>-0.035144</td>\n",
       "      <td>0.036847</td>\n",
       "      <td>-0.057527</td>\n",
       "      <td>-0.001652</td>\n",
       "      <td>0.012658</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Survived</th>\n",
       "      <td>-0.005007</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.338481</td>\n",
       "      <td>-0.077221</td>\n",
       "      <td>-0.035322</td>\n",
       "      <td>0.081629</td>\n",
       "      <td>0.257307</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pclass</th>\n",
       "      <td>-0.035144</td>\n",
       "      <td>-0.338481</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.369226</td>\n",
       "      <td>0.083081</td>\n",
       "      <td>0.018443</td>\n",
       "      <td>-0.549500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Age</th>\n",
       "      <td>0.036847</td>\n",
       "      <td>-0.077221</td>\n",
       "      <td>-0.369226</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.308247</td>\n",
       "      <td>-0.189119</td>\n",
       "      <td>0.096067</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SibSp</th>\n",
       "      <td>-0.057527</td>\n",
       "      <td>-0.035322</td>\n",
       "      <td>0.083081</td>\n",
       "      <td>-0.308247</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.414838</td>\n",
       "      <td>0.159651</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Parch</th>\n",
       "      <td>-0.001652</td>\n",
       "      <td>0.081629</td>\n",
       "      <td>0.018443</td>\n",
       "      <td>-0.189119</td>\n",
       "      <td>0.414838</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.216225</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Fare</th>\n",
       "      <td>0.012658</td>\n",
       "      <td>0.257307</td>\n",
       "      <td>-0.549500</td>\n",
       "      <td>0.096067</td>\n",
       "      <td>0.159651</td>\n",
       "      <td>0.216225</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             PassengerId  Survived    Pclass       Age     SibSp     Parch  \\\n",
       "PassengerId     1.000000 -0.005007 -0.035144  0.036847 -0.057527 -0.001652   \n",
       "Survived       -0.005007  1.000000 -0.338481 -0.077221 -0.035322  0.081629   \n",
       "Pclass         -0.035144 -0.338481  1.000000 -0.369226  0.083081  0.018443   \n",
       "Age             0.036847 -0.077221 -0.369226  1.000000 -0.308247 -0.189119   \n",
       "SibSp          -0.057527 -0.035322  0.083081 -0.308247  1.000000  0.414838   \n",
       "Parch          -0.001652  0.081629  0.018443 -0.189119  0.414838  1.000000   \n",
       "Fare            0.012658  0.257307 -0.549500  0.096067  0.159651  0.216225   \n",
       "\n",
       "                 Fare  \n",
       "PassengerId  0.012658  \n",
       "Survived     0.257307  \n",
       "Pclass      -0.549500  \n",
       "Age          0.096067  \n",
       "SibSp        0.159651  \n",
       "Parch        0.216225  \n",
       "Fare         1.000000  "
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.corr(numeric_only=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "ea01a0d4-3809-4c9e-9a18-90087edae2ef",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.3082467589236564"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"Age\"].corr(df[\"SibSp\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "1f7cf2f6-212f-4141-87d2-867875293583",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "74.00     1\n",
       "0.92      1\n",
       "32.50     2\n",
       "13.00     2\n",
       "46.00     3\n",
       "15.00     5\n",
       "54.00     8\n",
       "45.00    12\n",
       "16.00    17\n",
       "21.00    24\n",
       "Name: Age, dtype: int64"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"Age\"].value_counts(ascending=True)[::9]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "786ae068-7e0f-4ba2-bec2-e36dd7639428",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(64.084, 80.0]       11\n",
       "(48.168, 64.084]     69\n",
       "(0.339, 16.336]     100\n",
       "(32.252, 48.168]    188\n",
       "(16.336, 32.252]    346\n",
       "Name: Age, dtype: int64"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"Age\"].value_counts(ascending=True, bins=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "feed4470-43c8-4e28-805f-db2e1af63f13",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "714"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"Age\"].count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "70e235ca-b478-4010-9c03-e2e4145db55f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
